package com.dingwei.lesyndrome.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.dingwei.lesyndrome.R;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Andy.Li on 2017/3/16.
 */
public class FindCommentDialog extends Dialog {

    Activity activity;
    String name;

    public FindCommentDialog(Activity activity, String name) {
        super(activity, R.style.commentDialog);
        this.activity = activity;
        this.name = name;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.pop_comment);
        setCanceledOnTouchOutside(true);

        final TextView commit = (TextView) findViewById(R.id.tv_commit);
        final EditText edtContent = (EditText) findViewById(R.id.edt_comment);
        if (TextUtils.isEmpty(name)) {
            edtContent.setHint("发表评论");
        } else {
            edtContent.setHint("回复" + name + "：");
        }
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = edtContent.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    dismiss();
                    if (click != null) {
                        click.onCommit(content);
                    }
                } else {
                    Toast.makeText(activity, "评论不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        WindowManager.LayoutParams lp = getWindow().getAttributes(); // 获取对话框当前的参数值
        lp.width = activity.getResources().getDisplayMetrics().widthPixels; // 宽度
        getWindow().setAttributes(lp);
        getWindow().setGravity(Gravity.BOTTOM);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) edtContent
                        .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(edtContent, 0);
            }
        }, 50);
    }

    MyClick click;

    public void setClick(MyClick click) {
        this.click = click;
    }

    public interface MyClick {
        void onCommit(String content);
    }

}
